

#include "../Include/s51_io_i2c.h"

/**
 * @brief I2C启动时序
 **/
static void S51_I2C_Start(I2C_HandleTypeDef *hi2c)
{
    if (hi2c->Instance == HI2C_1)
    {
        pin_SDA_1_HIGH;
        pin_SCL_1_HIGH;
        pin_SDA_1_LOW;
        pin_SCL_1_LOW;
    }
}

/**
 * @brief I2C停止时序
 **/
static void S51_I2C_Stop(I2C_HandleTypeDef *hi2c)
{
    if (hi2c->Instance == HI2C_1)
    {
        pin_SDA_1_LOW;
        pin_SCL_1_HIGH;
        pin_SDA_1_HIGH;
    }
}

/**
 * @brief I2C主机应答时序
 **/
static void S51_I2C_Ack(I2C_HandleTypeDef *hi2c)
{
    if (hi2c->Instance == HI2C_1)
    {
        pin_SDA_1_LOW;
        pin_SCL_1_HIGH;
        pin_SCL_1_LOW;
        pin_SDA_1_HIGH;
    }
}

/**
 * @brief I2C主机拒绝应答时序
 **/
static void S51_I2C_Noack(I2C_HandleTypeDef *hi2c)
{
    if (hi2c->Instance == HI2C_1)
    {
        pin_SDA_1_HIGH;
        pin_SCL_1_HIGH;
        pin_SCL_1_LOW;
        pin_SDA_1_HIGH;
    }
}

/**
 * @brief I2C发送1个字节
 * @return 返回0通信失败,返回1通信成功
 **/
static uint8_t S51_I2C_WriteByte(I2C_HandleTypeDef *hi2c, uint8_t _data)
{
    uint8_t i, _ackflag;
    if (hi2c->Instance == HI2C_1)
    {
        for (i = 0; i < 8; i++)
        {

            if ((_data >> (7 - i) & 0x01) == HIGH)
            {
                pin_SDA_1_HIGH;
            }
            else
            {
                pin_SDA_1_LOW;
            }
            pin_SCL_1_HIGH;
            pin_SCL_1_LOW;
        }
        pin_SDA_1_HIGH;
        pin_SCL_1_HIGH;
        if (pin_SDA_1_READ() == HIGH) /*没有被拉低,通讯失败*/
        {
            _ackflag = HAL_I2C_CONNECT_FAILED;
        }
        else
        {
            _ackflag = HAL_I2C_CONNECT_SUCCESS;
        }
        pin_SCL_1_LOW;
        return _ackflag;

        return _ackflag;
    }
}

/**
 * @brief I2C读取一个字节
 * 
 * @param hi2c 
 * @return uint8_t 返回读取的一个字节
 */
static uint8_t S51_I2C_ReadByte(I2C_HandleTypeDef *hi2c)
{
    uint8_t i, _cache = 0;
    if (hi2c->Instance == HI2C_1)
    {
        for (i = 0; i < 8; i++)
        {
            _cache <<= 1;
            if (pin_SDA_1_READ() == HIGH)
            {
                _cache |= 1;
            }
            else
            {
                _cache |= 0;
            }
            pin_SCL_1_HIGH;
            pin_SCL_1_LOW;
        }
        return _cache;
    }
}


/**
 * @brief I2C主机发送数据,使用IO模拟
 * 
 * @param hi2c 
 * @param _address 
 * @param _pData 
 * @param _datasize 
 * @param _timeout 
 * @return uint8_t 
 */
uint8_t S51_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint8_t _address, uint8_t *_pData, uint8_t _datasize, uint8_t _timeout)
{
    uint8_t i;
    if (hi2c->Instance == HI2C_1)
    {
        S51_I2C_Start();
        if (S51_I2C_WriteByte(hi2c, _address) == HAL_I2C_CONNECT_FAILED)
        {
            S51_I2C_Stop();
            return HAL_I2C_CONNECT_FAILED;
        }
        for (i = 0; i < _datasize; i++)
        {
            if (S51_I2C_WriteByte(hi2c, *_pData) == HAL_I2C_CONNECT_FAILED)
            {
                S51_I2C_Stop();
                return HAL_I2C_CONNECT_FAILED;
            }
            else
            {
                _pData++;
            }
        }
        S51_I2C_Stop();
        return HAL_I2C_CONNECT_SUCCESS;
    }
}

/**
 * @brief 
 * 
 * @param hi2c 
 * @param _address 
 * @param _pData 
 * @param _datasize 
 * @param _timeout 
 * @return uint8_t 
 */
uint8_t S51_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint8_t _address, uint8_t *_pData, uint8_t _datasize, uint8_t _timeout)
{
}

/**
 * @brief I2C直接操作设备,设备的读写地址为自动增加
 * 
 * @param _Devadd 要操作的设置硬件地址
 * @param _pData 要读写数据的指针
 * @param _datasize 要发送的数据长度
 * @return int8_t 返回0通信失败,返回1通信成功
 */
int8_t S51_I2C_Mem_Write(uint8_t _Devadd, uint8_t *_pData, uint8_t _datasize)
{
    uint8_t _count;
    S51_I2C_Start();
    if (S51_I2C_WriteByte(_Devadd) == S51_I2C_CONNECT_FAILED) /*通信失败了*/
    {
        S51_I2C_Stop();
        return S51_I2C_CONNECT_FAILED;
    }
    for (_count = 0; _count < _datasize; _count++)
    {
        if (S51_I2C_WriteByte(*_pData) == S51_I2C_CONNECT_FAILED) /*通信失败了*/
        {
            S51_I2C_Stop();
            return S51_I2C_CONNECT_FAILED;
        }
        _pData++;
    }
    S51_I2C_Stop();
    return 0;
}

/**
 * @brief I2C直接操作设备,设备的读写地址为自动增加
 * @param _Devadd 要操作的设置硬件地址
 * @param _pData 要读写数据的指针
 * @param _datasize 要发送的数据长度
 * @return 返回0通信失败,返回1通信成功
 **/
int8_t S51_I2C_Mem_Read(uint8_t _Devadd, uint8_t *_pData, uint8_t _datasize)
{
    uint8_t _count;
    S51_I2C_Start();
    if (S51_I2C_WriteByte(_Devadd + 1) == S51_I2C_CONNECT_FAILED) /*通信失败了*/
    {
        S51_I2C_Start();
        return S51_I2C_CONNECT_FAILED;
    }
    for (_count = 0; _count < _datasize; _count++)
    {
        *_pData = S51_I2C_ReadByte();
        if (_count < _datasize - 1)
        {
            S51_I2C_Ack();
        }
        else
        {
            S51_I2C_Noack();
        }
        _pData++;
    }
    S51_I2C_Stop();
    return S51_I2C_CONNECT_SUCCESS;
}
